{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1 模块导入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# -*- coding: utf-8 -*-\n",
    "from sklearn.svm import SVC\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn import preprocessing  # 预处理模块\n",
    "from sklearn.preprocessing import LabelEncoder\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import time\n",
    "import joblib\n",
    "from sklearn.linear_model import Lasso, LassoLarsIC\n",
    "\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#不用print即可输出多个变量\n",
    "from IPython.core.interactiveshell import InteractiveShell\n",
    "InteractiveShell.ast_node_interactivity = \"all\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2 数据导入与预处理"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.1 数据导入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "#定义加载数据集的方法\n",
    "def load_dataset(path,inputfile):\n",
    "    return pd.read_csv(path+inputfile)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "path='data/zombie_enterprise_classification/'\n",
    "#加载训练集\n",
    "base_train=load_dataset(path,'base-train.csv')\n",
    "know_train=load_dataset(path,'knowledge-train.csv')\n",
    "money_train=load_dataset(path,'money-train.csv')\n",
    "year_train=load_dataset(path,'year-train.csv')\n",
    "\n",
    "#加载无标签数据集\n",
    "base_test=load_dataset(path,'base-test.csv')\n",
    "know_test=load_dataset(path,'knowledge-test.csv')\n",
    "money_test=load_dataset(path,'money-test.csv')\n",
    "year_test=load_dataset(path,'year-test.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "base_train: (28519, 9)\n",
      "know_train: (28519, 4)\n",
      "money_train (85548, 10)\n",
      "year_train (85548, 10)\n",
      "base_test: (7132, 8)\n",
      "know_test: (7132, 4)\n",
      "money_test (21396, 10)\n",
      "year_test (21396, 10)\n"
     ]
    }
   ],
   "source": [
    "#数据维度\n",
    "print('base_train:',base_train.shape)\n",
    "print('know_train:',know_train.shape)\n",
    "print('money_train',money_train.shape)\n",
    "print('year_train',year_train.shape)\n",
    "print('base_test:',base_test.shape)\n",
    "print('know_test:',know_test.shape)\n",
    "print('money_test',money_test.shape)\n",
    "print('year_test',year_test.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.2 数据预处理"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### 3.2.1 财报、年报数据的year特征填充"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数据集：year_train\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "ID           0\n",
       "year       856\n",
       "从业人数       805\n",
       "资产总额       927\n",
       "负债总额       828\n",
       "营业总收入      876\n",
       "主营业务收入     845\n",
       "利润总额       849\n",
       "纳税总额       817\n",
       "所有者权益合计    875\n",
       "dtype: int64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "----------------------------------------\n",
      "数据集：money_train\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "ID               0\n",
       "year           845\n",
       "债权融资额度         809\n",
       "债权融资成本         881\n",
       "股权融资额度         849\n",
       "股权融资成本         876\n",
       "内部融资和贸易融资额度    808\n",
       "内部融资和贸易融资成本    828\n",
       "项目融资和政策融资额度    862\n",
       "项目融资和政策融资成本    853\n",
       "dtype: int64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "----------------------------------------\n",
      "数据集：year_test\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "ID           0\n",
       "year       210\n",
       "从业人数       217\n",
       "资产总额       223\n",
       "负债总额       188\n",
       "营业总收入      211\n",
       "主营业务收入     211\n",
       "利润总额       185\n",
       "纳税总额       198\n",
       "所有者权益合计    229\n",
       "dtype: int64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "----------------------------------------\n",
      "数据集：money_test\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "ID               0\n",
       "year           228\n",
       "债权融资额度         223\n",
       "债权融资成本         211\n",
       "股权融资额度         208\n",
       "股权融资成本         213\n",
       "内部融资和贸易融资额度    196\n",
       "内部融资和贸易融资成本    213\n",
       "项目融资和政策融资额度    226\n",
       "项目融资和政策融资成本    229\n",
       "dtype: int64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "----------------------------------------\n"
     ]
    }
   ],
   "source": [
    "print('数据集：year_train')\n",
    "year_train.isna().sum()\n",
    "print('-'*40)\n",
    "print('数据集：money_train')\n",
    "money_train.isna().sum()\n",
    "print('-'*40)\n",
    "print('数据集：year_test')\n",
    "year_test.isna().sum()\n",
    "print('-'*40)\n",
    "print('数据集：money_test')\n",
    "money_test.isna().sum()\n",
    "print('-'*40)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>year</th>\n",
       "      <th>从业人数</th>\n",
       "      <th>资产总额</th>\n",
       "      <th>负债总额</th>\n",
       "      <th>营业总收入</th>\n",
       "      <th>主营业务收入</th>\n",
       "      <th>利润总额</th>\n",
       "      <th>纳税总额</th>\n",
       "      <th>所有者权益合计</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>990</td>\n",
       "      <td>2015.0</td>\n",
       "      <td>863.0</td>\n",
       "      <td>33760.0</td>\n",
       "      <td>25320.0</td>\n",
       "      <td>145168.0</td>\n",
       "      <td>58067.2</td>\n",
       "      <td>14516.8</td>\n",
       "      <td>0.0</td>\n",
       "      <td>8440.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>111</th>\n",
       "      <td>990</td>\n",
       "      <td>NaN</td>\n",
       "      <td>177.0</td>\n",
       "      <td>16880.0</td>\n",
       "      <td>4220.0</td>\n",
       "      <td>75960.0</td>\n",
       "      <td>30384.0</td>\n",
       "      <td>7596.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>12660.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>219</th>\n",
       "      <td>990</td>\n",
       "      <td>2017.0</td>\n",
       "      <td>880.0</td>\n",
       "      <td>75960.0</td>\n",
       "      <td>101280.0</td>\n",
       "      <td>372204.0</td>\n",
       "      <td>260542.8</td>\n",
       "      <td>37220.4</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-25320.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      ID    year   从业人数     资产总额      负债总额     营业总收入    主营业务收入     利润总额  纳税总额  \\\n",
       "3    990  2015.0  863.0  33760.0   25320.0  145168.0   58067.2  14516.8   0.0   \n",
       "111  990     NaN  177.0  16880.0    4220.0   75960.0   30384.0   7596.0   0.0   \n",
       "219  990  2017.0  880.0  75960.0  101280.0  372204.0  260542.8  37220.4   0.0   \n",
       "\n",
       "     所有者权益合计  \n",
       "3     8440.0  \n",
       "111  12660.0  \n",
       "219 -25320.0  "
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "show121=findIdLackOfYear(year_train)\n",
    "year_train[year_train['ID'].isin([show121[0]])]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "#找到缺失年份的ID\n",
    "def findIdLackOfYear(df,col_id='ID',col_lack='year'):\n",
    "    return df[col_id][df[col_lack].isna()].values\n",
    "#通过ID填补缺失的年份\n",
    "def fillYearById(df,na_year_id,col_id='ID',col_lack='year'):\n",
    "    res=pd.DataFrame(columns=df.columns.values)\n",
    "    for idx in na_year_id:\n",
    "        temp=df[df[col_id].isin([idx])]\n",
    "        #若年份只缺失了一个，则根据另外两个反推\n",
    "        if temp[col_lack].isna().sum()==1:\n",
    "            if not temp[col_lack].isin([2015]).any():\n",
    "                temp[col_lack][temp[col_lack].isna()]=2015            \n",
    "            elif not temp[col_lack].isin([2016]).any():\n",
    "                temp[col_lack][temp[col_lack].isna()]=2016\n",
    "            elif not temp[col_lack].isin([2017]).any():\n",
    "                temp[col_lack][temp[col_lack].isna()]=2017\n",
    "        res=res.append(temp)\n",
    "    na_id=findIdLackOfYear(res)\n",
    "    res=res[~res[col_id].isin(na_id)]\n",
    "    return res\n",
    "#合并到year数据集\n",
    "def getFilledYear(df,res,col_id='ID'):\n",
    "    return df[~df[col_id].isin(res[col_id].values)].append(res)\n",
    "#精确填充ID缺失的年份\n",
    "def fillAndMerge(df,col_id='ID',col_lack='year'):\n",
    "    na_id=findIdLackOfYear(df)\n",
    "    fill=fillYearById(df,na_id)\n",
    "    return getFilledYear(df,fill)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "#填充train的年份缺失值\n",
    "year_train321=fillAndMerge(year_train)\n",
    "money_train321=fillAndMerge(money_train)\n",
    "#填充test的年份缺失值\n",
    "year_test321=fillAndMerge(year_test)\n",
    "money_test321=fillAndMerge(money_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "year_train (85548, 10)\n",
      "year_train321 (85548, 10)\n",
      "money_train (85548, 10)\n",
      "money_train321 (85548, 10)\n",
      "----------------------------------------\n",
      "year_test (21396, 10)\n",
      "year_test321 (21396, 10)\n",
      "money_test (21396, 10)\n",
      "money_test321 (21396, 10)\n"
     ]
    }
   ],
   "source": [
    "#填充后维度和原数据集是否一致\n",
    "print('year_train',year_train.shape)\n",
    "print('year_train321',year_train321.shape)\n",
    "print('money_train',money_train.shape)\n",
    "print('money_train321',money_train321.shape)\n",
    "print('-'*40)\n",
    "print('year_test',year_test.shape)\n",
    "print('year_test321',year_test321.shape)\n",
    "print('money_test',money_test.shape)\n",
    "print('money_test321',money_test321.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "填充后删除year特征仍有缺失值的样本\n",
    "可看出year特征扔含有少量缺失值，原因是该部分样本每个都缺失了不止1个year特征，此时无法精确填充，直接删除该部分样本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数据集：year_train321 (85548, 10)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "24"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "----------------------------------------\n",
      "数据集：money_train321 (85548, 10)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "----------------------------------------\n",
      "数据集：year_test321 (21396, 10)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "----------------------------------------\n",
      "数据集：money_test321 (21396, 10)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#填充后的缺失值统计\n",
    "print('数据集：year_train321',year_train321.shape)\n",
    "year_train321['year'].isna().sum()\n",
    "print('-'*40)\n",
    "print('数据集：money_train321',money_train321.shape)\n",
    "money_train321['year'].isna().sum()\n",
    "print('-'*40)\n",
    "print('数据集：year_test321',year_test321.shape)\n",
    "year_test321['year'].isna().sum()\n",
    "print('-'*40)\n",
    "print('数据集：money_test321',money_test321.shape)\n",
    "money_test321['year'].isna().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数据集：year_train321 (85524, 10)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "----------------------------------------\n",
      "数据集：money_train321 (85538, 10)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#删除yeart特征含有缺失值的样本\n",
    "year_train321=year_train321.dropna(subset=['year'])\n",
    "money_train321=money_train321.dropna(subset=['year'])\n",
    "#填充后的缺失值统计\n",
    "print('数据集：year_train321',year_train321.shape)\n",
    "year_train321['year'].isna().sum()\n",
    "print('-'*40)\n",
    "print('数据集：money_train321',money_train321.shape)\n",
    "money_train321['year'].isna().sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### 3.2.2 拆分数据集\n",
    "按year特征(2015,2016,2017)拆分财报money和年报year数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "#定义拆分数据集的方法\n",
    "def split_dataset_by(df,col='year'):\n",
    "    \"\"\"\n",
    "    df：要处理的数据集的dataframe\n",
    "    col：要按哪一列处理\n",
    "    return:拆分后的数据集dataframe组成的列表\n",
    "    \"\"\"\n",
    "    split_df=[]\n",
    "    col_values=np.sort(df[col].value_counts().index.values)#col这一列的唯一值\n",
    "    columns=df.columns.values.tolist()#df所有列名\n",
    "    columns.remove(col)#删除掉要拆分的这一列的名称col\n",
    "    #循环拆分数据集\n",
    "    for col_value in col_values:\n",
    "        col_value=int(col_value)\n",
    "        locals()[col+str(col_value)]=df[df[col].isin([col_value])].drop([col],axis=1)\n",
    "        new_columns=['ID']\n",
    "        for column in columns:\n",
    "            if column=='ID':\n",
    "                continue\n",
    "            new_columns.append(column+str(col_value))\n",
    "        locals()[col+str(col_value)].columns=new_columns#拆分出来的数据集重新命名它的列名称\n",
    "        split_df.append(locals()[col+str(col_value)])#处理完成的数据集添加到列表中\n",
    "    return split_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "#按年份拆分money_train,year_train,money_test,year-test四个数据集\n",
    "split_money_train=split_dataset_by(money_train321)\n",
    "split_year_train=split_dataset_by(year_train321)\n",
    "split_money_test=split_dataset_by(money_test321)\n",
    "split_year_test=split_dataset_by(year_test321)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### 3.2.3 合并拆分数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "#定义合并数据集的方法\n",
    "def merge_dataset(dfs,col='ID'):\n",
    "    \"\"\"\n",
    "    df：要处理的数据集的dataframe\n",
    "    col：要按哪一列处理\n",
    "    return:合并后的数据集dataframe\n",
    "    \"\"\"\n",
    "    df_res=pd.DataFrame()\n",
    "    for df in dfs:\n",
    "        if len(df_res)==0:\n",
    "            df_res=df\n",
    "        else:\n",
    "            df_res=pd.merge(df_res,df,on=col,how='outer')#按col列合并，outer表示保留两个表的信息\n",
    "    return df_res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "#分别合并money_train,year_train拆分后的数据集\n",
    "money_train_res=merge_dataset(split_money_train)\n",
    "year_train_res=merge_dataset(split_year_train)\n",
    "money_test_res=merge_dataset(split_money_test)\n",
    "year_test_res=merge_dataset(split_year_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数据集：money_train_res (28516, 25)\n",
      "数据集：year_train_res (28516, 25)\n",
      "数据集：money_test_res (7132, 25)\n",
      "数据集：year_test_res (7132, 25)\n"
     ]
    }
   ],
   "source": [
    "print('数据集：money_train_res',money_train_res.shape)\n",
    "print('数据集：year_train_res',year_train_res.shape)\n",
    "print('数据集：money_test_res',money_test_res.shape)\n",
    "print('数据集：year_test_res',year_test_res.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "#合并所有数据集为train,test\n",
    "train=merge_dataset([base_train,know_train,money_train_res,year_train_res])\n",
    "test=merge_dataset([base_test,know_test,money_test_res,year_test_res])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(28519, 60)"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "(7132, 59)"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>注册时间</th>\n",
       "      <th>注册资本</th>\n",
       "      <th>行业</th>\n",
       "      <th>区域</th>\n",
       "      <th>企业类型</th>\n",
       "      <th>控制人类型</th>\n",
       "      <th>控制人持股比例</th>\n",
       "      <th>flag</th>\n",
       "      <th>专利</th>\n",
       "      <th>...</th>\n",
       "      <th>纳税总额2016</th>\n",
       "      <th>所有者权益合计2016</th>\n",
       "      <th>从业人数2017</th>\n",
       "      <th>资产总额2017</th>\n",
       "      <th>负债总额2017</th>\n",
       "      <th>营业总收入2017</th>\n",
       "      <th>主营业务收入2017</th>\n",
       "      <th>利润总额2017</th>\n",
       "      <th>纳税总额2017</th>\n",
       "      <th>所有者权益合计2017</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5986361</td>\n",
       "      <td>2014.0</td>\n",
       "      <td>7090.0</td>\n",
       "      <td>服务业</td>\n",
       "      <td>湖北</td>\n",
       "      <td>有限责任公司</td>\n",
       "      <td>自然人</td>\n",
       "      <td>0.93</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>7090.0</td>\n",
       "      <td>503.0</td>\n",
       "      <td>85080.0</td>\n",
       "      <td>155980.0</td>\n",
       "      <td>187176.0</td>\n",
       "      <td>149740.8</td>\n",
       "      <td>74870.4</td>\n",
       "      <td>37435.2</td>\n",
       "      <td>-70900.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5991749</td>\n",
       "      <td>2007.0</td>\n",
       "      <td>5940.0</td>\n",
       "      <td>零售业</td>\n",
       "      <td>湖南</td>\n",
       "      <td>合伙企业</td>\n",
       "      <td>企业法人</td>\n",
       "      <td>0.57</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>...</td>\n",
       "      <td>8553.6</td>\n",
       "      <td>109890.0</td>\n",
       "      <td>514.0</td>\n",
       "      <td>178200.0</td>\n",
       "      <td>344520.0</td>\n",
       "      <td>267300.0</td>\n",
       "      <td>106920.0</td>\n",
       "      <td>80190.0</td>\n",
       "      <td>160380.0</td>\n",
       "      <td>-166320.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>5998154</td>\n",
       "      <td>2002.0</td>\n",
       "      <td>9720.0</td>\n",
       "      <td>工业</td>\n",
       "      <td>福建</td>\n",
       "      <td>合伙企业</td>\n",
       "      <td>自然人</td>\n",
       "      <td>0.74</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>...</td>\n",
       "      <td>10886.4</td>\n",
       "      <td>72900.0</td>\n",
       "      <td>891.0</td>\n",
       "      <td>729000.0</td>\n",
       "      <td>719280.0</td>\n",
       "      <td>1458000.0</td>\n",
       "      <td>729000.0</td>\n",
       "      <td>729000.0</td>\n",
       "      <td>291600.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>5984390</td>\n",
       "      <td>2000.0</td>\n",
       "      <td>4800.0</td>\n",
       "      <td>商业服务业</td>\n",
       "      <td>山东</td>\n",
       "      <td>股份有限公司</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.90</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>61440.0</td>\n",
       "      <td>156000.0</td>\n",
       "      <td>819.0</td>\n",
       "      <td>326400.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>163200.0</td>\n",
       "      <td>65280.0</td>\n",
       "      <td>48960.0</td>\n",
       "      <td>65280.0</td>\n",
       "      <td>165600.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5980535</td>\n",
       "      <td>2004.0</td>\n",
       "      <td>4530.0</td>\n",
       "      <td>零售业</td>\n",
       "      <td>广东</td>\n",
       "      <td>农民专业合作社</td>\n",
       "      <td>自然人</td>\n",
       "      <td>0.95</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>65232.0</td>\n",
       "      <td>4530.0</td>\n",
       "      <td>158.0</td>\n",
       "      <td>176670.0</td>\n",
       "      <td>258210.0</td>\n",
       "      <td>494676.0</td>\n",
       "      <td>296805.6</td>\n",
       "      <td>197870.4</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-81540.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 60 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        ID    注册时间    注册资本     行业  区域     企业类型 控制人类型  控制人持股比例  flag   专利  ...  \\\n",
       "0  5986361  2014.0  7090.0    服务业  湖北   有限责任公司   自然人     0.93     0  0.0  ...   \n",
       "1  5991749  2007.0  5940.0    零售业  湖南     合伙企业  企业法人     0.57     0  1.0  ...   \n",
       "2  5998154  2002.0  9720.0     工业  福建     合伙企业   自然人     0.74     0  1.0  ...   \n",
       "3  5984390  2000.0  4800.0  商业服务业  山东   股份有限公司   NaN     0.90     0  0.0  ...   \n",
       "4  5980535  2004.0  4530.0    零售业  广东  农民专业合作社   自然人     0.95     0  0.0  ...   \n",
       "\n",
       "   纳税总额2016  所有者权益合计2016  从业人数2017  资产总额2017  负债总额2017  营业总收入2017  主营业务收入2017  \\\n",
       "0       0.0       7090.0     503.0   85080.0  155980.0   187176.0    149740.8   \n",
       "1    8553.6     109890.0     514.0  178200.0  344520.0   267300.0    106920.0   \n",
       "2   10886.4      72900.0     891.0  729000.0  719280.0  1458000.0    729000.0   \n",
       "3   61440.0     156000.0     819.0  326400.0       NaN   163200.0     65280.0   \n",
       "4   65232.0       4530.0     158.0  176670.0  258210.0   494676.0    296805.6   \n",
       "\n",
       "   利润总额2017  纳税总额2017  所有者权益合计2017  \n",
       "0   74870.4   37435.2     -70900.0  \n",
       "1   80190.0  160380.0    -166320.0  \n",
       "2  729000.0  291600.0          NaN  \n",
       "3   48960.0   65280.0     165600.0  \n",
       "4  197870.4       0.0     -81540.0  \n",
       "\n",
       "[5 rows x 60 columns]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train.shape\n",
    "test.shape\n",
    "train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "#train和test特征对齐\n",
    "cols323=test.columns.values.tolist()\n",
    "cols323.append('flag')\n",
    "train323=train.loc[:,cols323].copy()\n",
    "test323=test"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### 3.2.4 保存总数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "path324='data/zombie_enterprise_classification/alldata/'\n",
    "train323.to_csv(path324+'train324.txt',index=False)\n",
    "test323.to_csv(path324+'test324.txt',index=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.3 特征工程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "path33='data/zombie_enterprise_classification/alldata/'\n",
    "train33=pd.read_csv(path33+'train324.txt')\n",
    "test33=pd.read_csv(path33+'test324.txt')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### 3.3.1 ID特征处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(28519, 60)\n",
      "(7132, 59)\n"
     ]
    }
   ],
   "source": [
    "#处理前形状\n",
    "print(train33.shape)\n",
    "print(test33.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_Id = train33['ID']\n",
    "test_Id = test33['ID']\n",
    "train33.drop('ID', axis=1, inplace=True)\n",
    "test33.drop('ID', axis=1, inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(28519, 59)\n",
      "(7132, 58)\n"
     ]
    }
   ],
   "source": [
    "#处理后形状\n",
    "print(train33.shape)\n",
    "print(test33.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### 3.3.2 数据集连接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "all_data size is (35651, 58)\n"
     ]
    }
   ],
   "source": [
    "ntrain = train33.shape[0]\n",
    "ntest = test33.shape[0]\n",
    "y_train = train33['flag'].values\n",
    "all_data = pd.concat((train33, test33)).reset_index(drop=True)\n",
    "all_data.drop(['flag'], axis=1, inplace=True)\n",
    "print('all_data size is {}'.format(all_data.shape))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### 3.3.3 缺失值处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "注册时间               371\n",
       "注册资本               366\n",
       "行业                 323\n",
       "区域                 331\n",
       "企业类型               366\n",
       "控制人类型              387\n",
       "控制人持股比例            360\n",
       "专利                 351\n",
       "商标                 377\n",
       "著作权                339\n",
       "债权融资额度2015         369\n",
       "债权融资成本2015         386\n",
       "股权融资额度2015         369\n",
       "股权融资成本2015         369\n",
       "内部融资和贸易融资额度2015    344\n",
       "内部融资和贸易融资成本2015    378\n",
       "项目融资和政策融资额度2015    372\n",
       "项目融资和政策融资成本2015    345\n",
       "债权融资额度2016         346\n",
       "债权融资成本2016         364\n",
       "股权融资额度2016         343\n",
       "股权融资成本2016         372\n",
       "内部融资和贸易融资额度2016    352\n",
       "内部融资和贸易融资成本2016    349\n",
       "项目融资和政策融资额度2016    372\n",
       "项目融资和政策融资成本2016    379\n",
       "债权融资额度2017         344\n",
       "债权融资成本2017         369\n",
       "股权融资额度2017         372\n",
       "股权融资成本2017         375\n",
       "内部融资和贸易融资额度2017    335\n",
       "内部融资和贸易融资成本2017    341\n",
       "项目融资和政策融资额度2017    371\n",
       "项目融资和政策融资成本2017    385\n",
       "从业人数2015           349\n",
       "资产总额2015           400\n",
       "负债总额2015           344\n",
       "营业总收入2015          395\n",
       "主营业务收入2015         364\n",
       "利润总额2015           347\n",
       "纳税总额2015           340\n",
       "所有者权益合计2015        382\n",
       "从业人数2016           353\n",
       "资产总额2016           412\n",
       "负债总额2016           359\n",
       "营业总收入2016          355\n",
       "主营业务收入2016         355\n",
       "利润总额2016           350\n",
       "纳税总额2016           362\n",
       "所有者权益合计2016        406\n",
       "从业人数2017           353\n",
       "资产总额2017           371\n",
       "负债总额2017           345\n",
       "营业总收入2017          370\n",
       "主营业务收入2017         370\n",
       "利润总额2017           370\n",
       "纳税总额2017           345\n",
       "所有者权益合计2017        348\n",
       "dtype: int64"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#缺失值统计信息\n",
    "all_data.isna().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "#定义填充方法\n",
    "def fill_with_mode(df):\n",
    "    cols=df.columns\n",
    "    for col in cols:\n",
    "        df[col]=df[col].fillna(df[col].mode()[0])\n",
    "    return df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "#用众数填补缺失值\n",
    "# train322=fill_with_mode(train31)\n",
    "# test322=fill_with_mode(test31)\n",
    "all_data333=fill_with_mode(all_data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### 3.2.4 独热编码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>注册时间</th>\n",
       "      <th>注册资本</th>\n",
       "      <th>控制人持股比例</th>\n",
       "      <th>专利</th>\n",
       "      <th>商标</th>\n",
       "      <th>著作权</th>\n",
       "      <th>债权融资额度2015</th>\n",
       "      <th>债权融资成本2015</th>\n",
       "      <th>股权融资额度2015</th>\n",
       "      <th>股权融资成本2015</th>\n",
       "      <th>...</th>\n",
       "      <th>区域_湖北</th>\n",
       "      <th>区域_湖南</th>\n",
       "      <th>区域_福建</th>\n",
       "      <th>企业类型_农民专业合作社</th>\n",
       "      <th>企业类型_合伙企业</th>\n",
       "      <th>企业类型_有限责任公司</th>\n",
       "      <th>企业类型_股份有限公司</th>\n",
       "      <th>企业类型_集体所有制企业</th>\n",
       "      <th>控制人类型_企业法人</th>\n",
       "      <th>控制人类型_自然人</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2014.0</td>\n",
       "      <td>7090.0</td>\n",
       "      <td>0.93</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2007.0</td>\n",
       "      <td>5940.0</td>\n",
       "      <td>0.57</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>45144.0</td>\n",
       "      <td>1805.760</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2002.0</td>\n",
       "      <td>9720.0</td>\n",
       "      <td>0.74</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2000.0</td>\n",
       "      <td>4800.0</td>\n",
       "      <td>0.90</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>21312.0</td>\n",
       "      <td>852.480</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2004.0</td>\n",
       "      <td>4530.0</td>\n",
       "      <td>0.95</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>13952.4</td>\n",
       "      <td>558.096</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 74 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     注册时间    注册资本  控制人持股比例   专利   商标  著作权  债权融资额度2015  债权融资成本2015  股权融资额度2015  \\\n",
       "0  2014.0  7090.0     0.93  0.0  0.0  0.0         0.0         0.0         0.0   \n",
       "1  2007.0  5940.0     0.57  1.0  1.0  0.0         0.0         0.0     45144.0   \n",
       "2  2002.0  9720.0     0.74  1.0  1.0  0.0         0.0         0.0         0.0   \n",
       "3  2000.0  4800.0     0.90  0.0  0.0  0.0         0.0         0.0     21312.0   \n",
       "4  2004.0  4530.0     0.95  0.0  1.0  1.0         0.0         0.0     13952.4   \n",
       "\n",
       "   股权融资成本2015  ...  区域_湖北  区域_湖南  区域_福建  企业类型_农民专业合作社  企业类型_合伙企业  企业类型_有限责任公司  \\\n",
       "0       0.000  ...      1      0      0             0          0            1   \n",
       "1    1805.760  ...      0      1      0             0          1            0   \n",
       "2       0.000  ...      0      0      1             0          1            0   \n",
       "3     852.480  ...      0      0      0             0          0            0   \n",
       "4     558.096  ...      0      0      0             1          0            0   \n",
       "\n",
       "   企业类型_股份有限公司  企业类型_集体所有制企业  控制人类型_企业法人  控制人类型_自然人  \n",
       "0            0             0           0          1  \n",
       "1            0             0           1          0  \n",
       "2            0             0           0          1  \n",
       "3            1             0           1          0  \n",
       "4            0             0           0          1  \n",
       "\n",
       "[5 rows x 74 columns]"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "all_data334 = pd.get_dummies(all_data333)\n",
    "all_data334.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### 3.2.5 标签编码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "#待编码的特征\n",
    "cols_encode=['注册时间']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "#将数值类型（数值作为类型的列）转化为字符串类型\n",
    "feature=['注册时间']\n",
    "for i in feature:\n",
    "    train[i]=train[i].astype(str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "#对不连续的数字或文本进行编号，转换成连续的数值型变量\n",
    "for c in cols_encode:\n",
    "    le=LabelEncoder()\n",
    "    all_data334[c]=le.fit_transform(list(all_data334[c].values))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>注册时间</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   注册时间\n",
       "0    14\n",
       "1     7\n",
       "2     2\n",
       "3     0\n",
       "4     4"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#查看编码后的情况\n",
    "all_data334[cols_encode].head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### 3.2.6 重新划分数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将处理好的数据重新划分成训练集和测试集，为模型训练与测试做准备\n",
    "train326 = all_data334[:ntrain]\n",
    "test326 = all_data334[ntrain:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### 3.2.6 特征重要性检测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Lasso(alpha=0.001, copy_X=True, fit_intercept=True, max_iter=1000,\n",
       "      normalize=False, positive=False, precompute=False, random_state=None,\n",
       "      selection='cyclic', tol=0.0001, warm_start=False)"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lasso=Lasso(alpha=0.001)\n",
    "lasso.fit(train326,y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>特征重要性</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>企业类型_有限责任公司</th>\n",
       "      <td>0.016948</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>区域_江西</th>\n",
       "      <td>0.003585</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>行业_工业</th>\n",
       "      <td>0.001465</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>项目融资和政策融资成本2017</th>\n",
       "      <td>0.000110</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>项目融资和政策融资成本2015</th>\n",
       "      <td>0.000096</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>注册时间</th>\n",
       "      <td>-0.001867</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>区域_广东</th>\n",
       "      <td>-0.002932</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>企业类型_合伙企业</th>\n",
       "      <td>-0.004633</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>著作权</th>\n",
       "      <td>-0.004877</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>行业_服务业</th>\n",
       "      <td>-0.006270</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>74 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                    特征重要性\n",
       "企业类型_有限责任公司      0.016948\n",
       "区域_江西            0.003585\n",
       "行业_工业            0.001465\n",
       "项目融资和政策融资成本2017  0.000110\n",
       "项目融资和政策融资成本2015  0.000096\n",
       "...                   ...\n",
       "注册时间            -0.001867\n",
       "区域_广东           -0.002932\n",
       "企业类型_合伙企业       -0.004633\n",
       "著作权             -0.004877\n",
       "行业_服务业          -0.006270\n",
       "\n",
       "[74 rows x 1 columns]"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "FI_lasso = pd.DataFrame({\"特征重要性\":lasso.coef_}, index=train326.columns)\n",
    "FI_lasso.sort_values(\"特征重要性\",ascending=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4 模型定义、训练、评估 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用SVM作为模型时，通常采用如下流程：\n",
    "\n",
    "- 对样本数据进行归一化\n",
    "- 应用核函数对样本进行映射\n",
    "- 用cross-validation和grid-search对超参数进行优选\n",
    "- 用最优参数训练得到模型\n",
    "- 使用训练好的模型进行预测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "正例： 13933\n",
      "负例： 8882\n"
     ]
    }
   ],
   "source": [
    "#训练集向量\n",
    "X=train326.values\n",
    "y=y_train\n",
    "\n",
    "#预测数据集向量\n",
    "X_pre=test326.values\n",
    "\n",
    "#按8:2分割数据集为训练集和测试集，固定随机种子\n",
    "X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=2020)\n",
    "\n",
    "#对数据集进行标准化\n",
    "scaler=preprocessing.StandardScaler().fit(X_train)#通过训练集获得归一化函数\n",
    "\n",
    "#使用相同的归一化函数处理训练向量和测试向量，以及无标签测试向量\n",
    "X_train=scaler.transform(X_train)\n",
    "X_test=scaler.transform(X_test)\n",
    "X_pre=scaler.transform(X_pre)\n",
    "\n",
    "#训练集中正例和负例个数\n",
    "y_list=y_train.tolist()\n",
    "print(\"正例：\",y_list.count(0))\n",
    "print(\"负例：\",y_list.count(1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "SVM rbf核函数，C，gamma参数调优\n",
    "grid = GridSearchCV(SVC(), param_grid={\"C\":[0.1, 1, 10], \"gamma\":[1, 0.1, 0.01]}, cv=4)  \n",
    "grid.fit(X_train, y_train)  \n",
    "print(\"The best parameters are %s with a score of %0.2f\" %(grid.best_params_, grid.best_score_))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "SVC(C=10, cache_size=200, class_weight=None, coef0=0.0,\n",
       "    decision_function_shape='ovr', degree=3, gamma=0.01, kernel='rbf',\n",
       "    max_iter=-1, probability=False, random_state=None, shrinking=True,\n",
       "    tol=0.001, verbose=False)"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练时间： 33.007998s\n",
      "支持向量： [2296 3329]\n",
      "正例： 2296\n",
      "负例： 3329\n"
     ]
    }
   ],
   "source": [
    "#SVM模型训练\n",
    "clf=SVC(C=10,gamma=0.01)\n",
    "t1=time.time()\n",
    "clf.fit(X_train,y_train)\n",
    "t2=time.time()\n",
    "print(\"训练时间： %fs\"%(t2-t1))\n",
    "sv=clf.n_support_\n",
    "print(\"支持向量：\",sv)\n",
    "\n",
    "#支持向量中正例负例个数\n",
    "print(\"正例：\",sv[0])\n",
    "print(\"负例：\",sv[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "准确率: 0.9665147265077139\n"
     ]
    }
   ],
   "source": [
    "accur=clf.score(X_test,y_test)\n",
    "print(\"准确率:\",accur)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['data//zombie_enterprise_classification/model/svc0513.model']"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#保存模型\n",
    "joblib.dump(clf,'data//zombie_enterprise_classification/model/svc0513.model')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 预测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_pre=clf.predict(X_pre).tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>flag</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5991927</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5998351</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>5992703</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>5979231</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5995422</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7127</th>\n",
       "      <td>80564</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7128</th>\n",
       "      <td>978515</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7129</th>\n",
       "      <td>568065</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7130</th>\n",
       "      <td>1889883</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7131</th>\n",
       "      <td>2507377</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>7132 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           ID  flag\n",
       "0     5991927     0\n",
       "1     5998351     0\n",
       "2     5992703     0\n",
       "3     5979231     0\n",
       "4     5995422     1\n",
       "...       ...   ...\n",
       "7127    80564     1\n",
       "7128   978515     1\n",
       "7129   568065     1\n",
       "7130  1889883     1\n",
       "7131  2507377     1\n",
       "\n",
       "[7132 rows x 2 columns]"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_res=pd.DataFrame()\n",
    "df_res['ID']=test_Id\n",
    "df_res['flag']=y_pre\n",
    "df_res=df_res.astype(int)\n",
    "df_res.to_csv(\"data//zombie_enterprise_classification/output/res0513.txt\",index=False)#输出到文件，不加行索引 \n",
    "df_res"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
